<?php
	class sanphamdb
	{
		public function Add($MaLoai, $MaChatLieu, $MaNhaSX, $MaNhomHang, $TenSanPham, $MoTa, $DonGia, $SoLuong,$MaKichThuoc, $MaKieuDang, $MauSac, $NgayDang, $LuotXem)
		{
			$sql = " INSERT INTO `tbl_sanpham`(`MaNhomHang`, `MaLoai`, `MaChatLieu`, `MaNhaSX`, `TenSanPham`, `MoTa`, `DonGia`, `SoLuong`, `MaKichThuoc`, `MaKieuDang`, `MauSac`, `NgayDang`, `LuotXem`) VALUES  ($MaNhomHang, $MaLoai, $MaChatLieu,$MaNhaSX, ".dataservice::slashes($TenSanPham).", ".dataservice::slashes($MoTa).", $DonGia, $SoLuong,$MaKichThuoc,$MaKieuDang,".dataservice::slashes($MauSac).", '$NgayDang', $LuotXem)";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function Delete($MaSanPham)
		{
			$sql = "DELETE FROM `tbl_sanpham` WHERE MaSanPham = $MaSanPham";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function Update($MaSanPham, $MaLoai, $MaChatLieu, $MaNhaSX, $MaNhomHang, $TenSanPham, $MoTa, $DonGia, $SoLuong, $MaKichThuoc, $MaKieuDang, $MauSac)
		{
			$sql = "UPDATE `tbl_sanpham` 
			SET `MaLoai` = '$MaLoai',
			`MaChatLieu` = '$MaChatLieu',
			`MaNhaSX` = '$MaNhaSX',
			`MaNhomHang`= '$MaNhomHang',
			`TenSanPham` = ".dataservice::slashes($TenSanPham).",
			`MoTa`=".dataservice::slashes($MoTa).",
			`DonGia` = $DonGia,
			`SoLuong` = $SoLuong,
			`MaKichThuoc`= '$MaKichThuoc',
			`MaKieuDang`= '$MaKieuDang',
			`MauSac` = '$MauSac'
			 WHERE MaSanPham= '$MaSanPham' "; 
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		public function Update_SoLuong($MaSanPham, $SoLuong)
		{
			$sql = "UPDATE `tbl_sanpham` SET `SoLuong`=$SoLuong WHERE MaSanPham= $MaSanPham"; 
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
	
		public function GetList($MaSanPham, $MaLoai, $MaChatLieu, $MaNhaSX, $MaNhomHang, $TenSanPham,
		 $MoTa, $DonGia, $SoLuong)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang, tbl_chatlieu, tbl_kichthuoc, tbl_kieudang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai AND tbl_sanpham.MaChatLieu = tbl_chatlieu.MaChatLieu AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang AND tbl_sanpham.MaKichThuoc = tbl_kichthuoc.MaKichThuoc AND tbl_sanpham.MaKieuDang = tbl_kieudang.MaKieuDang ";
			if($MaSanPham!="")
				$sql .= " AND MaSanPham = $MaSanPham ";
			if($MaLoai!="")
				$sql .= " AND MaLoai = $MaLoai ";
			if($MaChatLieu!="")
				$sql .= " AND MaChatLieu = $MaChatLieu ";
			if($MaNhaSX!="")
				$sql .= " AND MaNhaSX = $MaNhaSX ";
			if($MaNhomHang!="")
				$sql .= " AND MaNhomHang = $MaNhomHang ";
			if($TenSanPham!="")
				$sql .= " AND TenSanPham LIKE '%$TenSanPham%' ";
			if($MoTa!="")
				$sql .= " AND MoTa LIKE '%$MoTa%' ";
			if($DonGia!="")
				$sql .= " AND DonGia = $DonGia ";
			if($SoLuong!="")
				$sql .= " AND SoLuong = $SoLuong ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function GetList_New()
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang, tbl_chatlieu, tbl_kichthuoc, tbl_kieudang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai AND tbl_sanpham.MaChatLieu = tbl_chatlieu.MaChatLieu AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang AND tbl_sanpham.MaKichThuoc = tbl_kichthuoc.MaKichThuoc AND tbl_sanpham.MaKieuDang = tbl_kieudang.MaKieuDang ORDER BY tbl_sanpham.MaSanPham DESC ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function GetPageList_New($start, $limit)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang, tbl_chatlieu, tbl_kichthuoc, tbl_kieudang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai AND tbl_sanpham.MaChatLieu = tbl_chatlieu.MaChatLieu AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang AND tbl_sanpham.MaKichThuoc = tbl_kichthuoc.MaKichThuoc AND tbl_sanpham.MaKieuDang = tbl_kieudang.MaKieuDang ORDER BY tbl_sanpham.MaSanPham DESC LIMIT $start, $limit";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function GetList_Loaisp($MaLoai)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang, tbl_chatlieu, tbl_kichthuoc, tbl_kieudang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai AND tbl_sanpham.MaChatLieu = tbl_chatlieu.MaChatLieu AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang AND tbl_sanpham.MaKichThuoc = tbl_kichthuoc.MaKichThuoc AND tbl_sanpham.MaKieuDang = tbl_kieudang.MaKieuDang AND tbl_sanpham.Maloai = $MaLoai ORDER BY tbl_sanpham.MaSanPham DESC ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function GetList_MaSanPham($dsma)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang, tbl_chatlieu, tbl_kichthuoc, tbl_kieudang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai AND tbl_sanpham.MaChatLieu = tbl_chatlieu.MaChatLieu AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang AND tbl_sanpham.MaKichThuoc = tbl_kichthuoc.MaKichThuoc AND tbl_sanpham.MaKieuDang = tbl_kieudang.MaKieuDang AND MaSanPham in ($dsma)";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		public function Find($key, $loai)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang, tbl_chatlieu, tbl_kichthuoc, tbl_kieudang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai AND tbl_sanpham.MaChatLieu = tbl_chatlieu.MaChatLieu AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang AND tbl_sanpham.MaKichThuoc = tbl_kichthuoc.MaKichThuoc AND tbl_sanpham.MaKieuDang = tbl_kieudang.MaKieuDang " ;
			if($loai == 0) $sql.= " AND tbl_sanpham.TenSanPham LIKE '%$key%' ";
			else if($loai == 1) $sql.= " AND tbl_nhasanxuat.TenNhaSX LIKE '%$key%' ";
			else if($loai == 2) $sql.= " AND tbl_sanpham.MauSac LIKE '%$key%' ";
			else if($loai == 3) $sql.= " AND tbl_loaisp.TenLoai LIKE '%$key%' ";
			else if($loai == 4) $sql.= " AND tbl_chatlieu.TenChatLieu LIKE '%$key%' ";
			else if($loai == 5) $sql.= " AND tbl_nhomhang.TenNhomHang LIKE '%$key%' ";
			else if($loai == 6) $sql.= " AND tbl_sanpham.KichThuoc LIKE '%$key%' ";
			else if($loai == 7) $sql.= " AND tbl_sanpham.KieuDang LIKE '%$key%' ";
			
			$sql.= " GROUP BY tbl_sanpham.MaSanPham ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		public function PageList_Find($key, $loai, $start, $limit)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang, tbl_chatlieu, tbl_kichthuoc, tbl_kieudang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai AND tbl_sanpham.MaChatLieu = tbl_chatlieu.MaChatLieu AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang AND tbl_sanpham.MaKichThuoc = tbl_kichthuoc.MaKichThuoc AND tbl_sanpham.MaKieuDang = tbl_kieudang.MaKieuDang " ;
			if($loai == 0) $sql.= " AND tbl_sanpham.TenSanPham LIKE '%$key%' ";
			else if($loai == 1) $sql.= " AND tbl_nhasanxuat.TenNhaSX LIKE '%$key%' ";
			else if($loai == 2) $sql.= " AND tbl_sanpham.MauSac LIKE '%$key%' ";
			else if($loai == 3) $sql.= " AND tbl_loaisp.TenLoai LIKE '%$key%' ";
			else if($loai == 4) $sql.= " AND tbl_chatlieu.TenChatLieu LIKE '%$key%' ";
			else if($loai == 5) $sql.= " AND tbl_nhomhang.TenNhomHang LIKE '%$key%' ";
			else if($loai == 6) $sql.= " AND tbl_sanpham.KichThuoc LIKE '%$key%' ";
			else if($loai == 7) $sql.= " AND tbl_sanpham.KieuDang LIKE '%$key%' ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		public function AscNumView($MaSanPham)
		{
			$sql = "UPDATE `tbl_sanpham` SET `LuotXem` = `LuotXem` + 1 WHERE `MaSanPham` = $MaSanPham";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
	}
?>